package com.lwq.config;

import com.lwq.constants.Constants;
import com.lwq.filter.RepeatSubmitInterceptor;
import com.lwq.filter.TokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/6/10
 */
@Configuration
public class WebAppConfig implements WebMvcConfigurer {

    @Bean
    public TokenFilter getTokenFilter(){
        return new TokenFilter();
    }

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
    @Value("${token.excludePath}")
    private String excludePah;

    /**
     * token拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getTokenFilter()).addPathPatterns("/**")
                .excludePathPatterns(excludePah.split(","));
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + CoreConfig.getPath() + "/");
                registry.addResourceHandler("docs.html")
                        .addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
            }
        };
    }
}
